#!/usr/bin/env python3
#
#+-ext4_file_read_iter() <ssize_t ext4_file_read_iter (struct kiocb *iocb, struct iov_iter *to) at /data/tkernel4/fs/ext4/file.c:113>
#   +-file_inode()
#   +-unlikely() <if (unlikely ( ! page)) at /data/tkernel4/mm/page_alloc.c:2740>
#   +-ext4_forced_shutdown()
#   +-EXT4_SB()
#   +-iov_iter_count()
#   +-IS_DAX()
#   +-ext4_dax_read_iter() <ssize_t ext4_dax_read_iter (struct kiocb *iocb, struct iov_iter *to) at /data/tkernel4/fs/ext4/file.c:85>
#   | +-file_inode()
#   | +-inode_trylock_shared()
#   | +-inode_lock_shared()
#   | +-IS_DAX()
#   | +-inode_unlock_shared()
#   | +-generic_file_read_iter() <ssize_t generic_file_read_iter (struct kiocb *iocb, struct iov_iter *iter) at /data/tkernel4/mm/filemap.c:    2525>
#   | +-iov_iter_count()
#   | | +-i_size_read()
#第三个参数8:只输出第8行开始, 即ext4_dax_read_iter下一级level的函数, 
#第二个参数1:ext4_dax_read_iter level是parent level, 这里是1, 其child 是2, grandson是3,只输出level=2的函数;
#第一个参数来自于cflow生成的文件 func_tree.txt;
#./get_subfunc_level.py  func_tree.txt 1 8
#child: 2  > parent: 1  < grandson: 3 :: -file_inode()
#child: 2  > parent: 1  < grandson: 3 :: -inode_trylock_shared()
#child: 2  > parent: 1  < grandson: 3 :: -inode_lock_shared()
#child: 2  > parent: 1  < grandson: 3 :: -IS_DAX()
#child: 2  > parent: 1  < grandson: 3 :: -inode_unlock_shared()
#child: 2  > parent: 1  < grandson: 3 :: -generic_file_read_iter()
#child: 2  > parent: 1  < grandson: 3 :: -iov_iter_count()
import sys
import os


def parse_conf(_f):
    _ret = dict()
    with open(_f, 'r') as _fd:
        for _l in _fd.readlines():
            _ll = _l.strip()

            # skip null lines
            if not _ll:
                continue

            # skip comment lines
            if _ll.find("#") == 0:
                continue

            # get kv
            eidx = _ll.find("(")
            if eidx == -1:
                print("WARNNING: not = find in " + _ll);
                continue
            level = 0
            tmpidx = _ll.find("+-")
            if tmpidx == -1: 
                tmpidx = _ll.find("\-")
                if tmpidx != -1:
                    level = tmpidx
                    tmpidx = tmpidx+2
                    key = _ll[tmpidx:eidx].strip()
            else:
                level = tmpidx
                tmpidx = tmpidx+2
                key = _ll[tmpidx:eidx].strip()
            val = _ll[eidx+1:].strip()

            print("", level)
            break
    return _ret


def comp_conf(_kv1):
    _ret = dict()
    _ret['same'] = dict()
    _ret['diff'] = dict()
    _ret['miss'] = dict()
    miss=0
    for _k1, _v1 in _kv1.items():
        _tmp = dict()
        _ret['diff'][_k1] = {}
    return _ret


def output(result):
    for _k in ('miss', 'diff', 'same'):
        _v = result.get(_k, {})
        for __k, __v in _v.items():
            print("{:<40}".format(__v))
            break
    
if __name__ == "__main__":
    _f1 = sys.argv[1];

    _kv1 = parse_conf(_f1);
    ret = comp_conf(_kv1)
    #output(ret)
